package cn.mineserv.video.videoordersystem.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.*;
import java.nio.file.Path;

/**
 * @author 爱吃蛋白的丶圆周率
 */
public class FileUploadUtil {

    /**
     * 上传单一文件
     *
     * @param part
     * @param request
     * @return
     */
    public static String uploadSingleFile(Part part, HttpServletRequest request,Integer id) {
        String path = null;
        String header = part.getHeader("content-disposition");
        String fileName = getFileName(header);
        if (!"".equals(fileName)) {
            //得到上传文件的文件扩展名
            String fileExtName = getFileExtName(header);
            String newFileName = id +"_"+System.currentTimeMillis()+ fileExtName;
            //得到上传文件的保存路径
            String savePath = request.getServletContext().getRealPath("/image/book");
            String savePath2 = "D:\\ProjectAndResource\\Java\\IDEA\\MangaSystem\\web\\img\\book";
            
            
            File f = new File(savePath);
            File f2 = new File(savePath2);
            if (!f.exists()) {
                boolean mkdirs = f.mkdirs();
            }
            if (!f2.exists()) {
                boolean mkdirs = f2.mkdirs();
            }
            //文件上传
            try {
                Path path1 = f2.toPath();
                InputStream inputStream = part.getInputStream();
                OutputStream outputStream = new FileOutputStream(f2);
                int read = 0;
                byte[] bytes = new byte[1024 * 1024 * 1024];
                //先读后写
                while ((read = inputStream.read(bytes)) > 0){
                    byte[] wBytes = new byte[read];
                    System.arraycopy(bytes, 0, wBytes, 0, read);
                    outputStream.write(wBytes);
                }
                outputStream.flush();
                outputStream.close();
                inputStream.close();
                part.write(savePath + File.separator + newFileName);
                path = "img/book/" + newFileName;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            return path;
    }

    /**
     * 根据请求头解析出文件名
     * 请求头的格式：火狐和google浏览器下：form-data; name="file"; filename="snmp4j--api.zip"
     * IE浏览器下：form-data; name="file"; filename="E:\snmp4j--api.zip"
     *
     * @param header 请求头
     * @return 文件名
     */
    private static String getFileName(String header) {
        /**
         * String[] tempArr1 = header.split(";");代码执行完之后，在不同的浏览器下，tempArr1数组里面的内容稍有区别
         * 火狐或者google浏览器下：tempArr1={form-data,name="file",filename="snmp4j--api.zip"}
         * IE浏览器下：tempArr1={form-data,name="file",filename="E:\snmp4j--api.zip"}
         */
        String[] tempArr1 = header.split(";");
        /**
         *火狐或者google浏览器下：tempArr2={filename,"snmp4j--api.zip"}
         *IE浏览器下：tempArr2={filename,"E:\snmp4j--api.zip"}
         */
        String[] tempArr2 = tempArr1[2].split("=");
        //获取文件名，兼容各种浏览器的写法
        return tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
    }

    private static String getFileExtName(String header) {
        String fileName = getFileName(header);
        return fileName.substring(fileName.lastIndexOf("."));
    }
}
